import { getExtra, getRegexOptions } from './constant'
const columnMixin = {
  data () {
    return {
      regexOptions: getRegexOptions(),
      extra: getExtra(),
      name: '',
      code: '',
      compType: '',
      booleanDefaultVal: false,
      strDefaultVal: '',
      numberDefaultVal: 0,
      arrDefaultVal: [],
      isLike: false,
      regexType: '',
      regex: '',
      errorMsg: ''
    }
  },
  computed: {
    hasType () {
      return this.compType.length > 0 && ['字典', '多选'].includes(this.compType)
    },
    hasRows () {
      return this.compType.length > 0 && ['多行文本'].includes(this.compType)
    },
    hasHeight () {
      return this.compType.length > 0 && ['富文本'].includes(this.compType)
    },
    hasDefaultVal () {
      return this.compType.length > 0 && ['单行文本', '整数', '浮点数', '单选框', '多选', '字典', '开关'].includes(this.compType)
    }
  },
  watch: {
    regexType (v) {
      if (v === '') {
        this.regex = ''
        this.errorMsg = ''
      } else if (v === 'email') {
        this.regex = '\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}'
        this.errorMsg = '邮箱格式不正确'
      } else if (v === 'phone') {
        this.regex = '0?(13|14|15|17|18|19)[0-9]{9}'
        this.errorMsg = '手机号格式不正确'
      } else if (v === 'idcard') {
        this.regex = '\\d{17}[\\d|x]|\\d{15}'
        this.errorMsg = '请输入合法的身份证号码'
      } else if (v === 'diy') {
        this.regex = ''
        this.errorMsg = ''
      }
    }
  },
  methods: {
    reset () {
      this.name = ''
      this.code = ''
      this.compType = ''
      this.booleanDefaultVal = false
      this.strDefaultVal = ''
      this.numberDefaultVal = 0
      this.arrDefaultVal = []
      this.isLike = false
      this.regexType = ''
      this.regex = ''
      this.errorMsg = ''
    },
    isBoolean (compType) {
      return compType.length > 0 && ['单选框', '开关'].includes(compType)
    },
    getDefaultVal () {
      if (['单选框', '开关'].includes(this.compType)) {
        return this.booleanDefaultVal ? '1' : '0'
      } else if (['整数', '浮点数'].includes(this.compType)) {
        return `${this.numberDefaultVal}`
      } else if (['多选'].includes(this.compType)) {
        return this.arrDefaultVal.join(',')
      }
      return this.strDefaultVal
    },
    initDefaultVal (defaultVal) {
      if (['单选框', '开关'].includes(this.compType)) {
        this.booleanDefaultVal = defaultVal === '1'
      } else if (['整数', '浮点数'].includes(this.compType)) {
        this.numberDefaultVal = Number(defaultVal)
      }
      this.strDefaultVal = defaultVal
    }
  }
}
export {
  columnMixin
}
